import {useState, useEffect} from'react';

function useMouse(){
    

    const [x, setX] = useState(0);
    const [y, setY] = useState(0);

    const mouseMoveHandler = (event: MouseEvent)=>{
        setX(event.clientX);
        setY(event.clientY);
    }
 
    useEffect(()=>{
        // 监听鼠标事件
        window.addEventListener('mousemove', mouseMoveHandler)

        // 组件销毁时, 要解绑dom事件, 不然可能内存泄露 
        return ()=>{
            window.removeEventListener('mousemove', mouseMoveHandler)
        }
    }, [])
    // 组件销毁时

    return {x, y}
}

export default useMouse;